home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_146 / blanker2 / blanker2.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  221 lines

  1. /* 
  2.     Blanker2 -- by Joe Hitchens
  3.     v1.27.88
  4.     A screen blanking program that turns the screen black after 90 seconds
  5.     of keyboard and mouse inactivity.
  6.  
  7.     This program is PUBLIC DOMAIN.
  8.     Do me a favor though, and leave my name on it.
  9.  
  10.     Usage: blanker2 [n]
  11.         n = number of seconds of inactivity after which blanking occurs.
  12.         if n is not given, a default of 90 is used.
  13.         NOTE: blanker2 need not be 'run'.  It will install an event
  14.               handler and leave it behind when exiting.
  15.               Also, the CLI window it may have been run from can be 
  16.               closed after installation.
  17.  
  18.     Exit codes
  19.     0    Normal exit, everything went fine.
  20.     1    CreatePort() failed.
  21.     2    CreateStdIO() failed.
  22.     3    OpenDevice() on "input.device" failed.
  23.     4    Memory Allocation for Interrupt structure failed.
  24.     5    Memory Allocation for Blanker structure failed.
  25.     6    Memory Allocation for event handler code space failed.
  26.  
  27.     Please report any bugs you may find to me, so that I can try to
  28.     fix them.
  29.  
  30.     j.h.                      joe@sally.utexas.edu    
  31. */
  32.  
  33. #include <stdio.h>
  34. #include <exec/types.h>
  35. #include <exec/ports.h>
  36. #include <exec/memory.h>
  37. #include <exec/io.h>
  38. #include <exec/interrupts.h>
  39. #include <devices/input.h>
  40. #include <exec/devices.h>
  41. #include <devices/inputevent.h>
  42. #include <graphics/gfxmacros.h>
  43. #include <hardware/custom.h>
  44.  
  45. extern struct MsgPort    *CreatePort();
  46. extern struct IOStdReq    *CreateStdIO();
  47. extern char                *AllocMem();
  48. extern int                HandlerInterface();
  49.  
  50. #define DEFAULT_BLANKPOINT    900        /* This is 1/10's of seconds */
  51.  
  52. struct blanker {
  53.     long    Class;
  54.     long    Ticks;
  55.     long    BlankPoint;
  56.     long    Dark;
  57.     char    Name[8];
  58.     };
  59.  
  60. /*    ----------------------------------------
  61.     Actual entry point for event handler 
  62.     ---------------------------------------- */
  63. #asm
  64.     public  _HandlerInterface
  65.  
  66. _HandlerInterface:
  67.     movem.l    a0/a1,-(a7)    ;
  68.     bsr    _myhandler    ;go to the C language routine we provided
  69.     addq.l    #8,a7        ;
  70.     rts
  71. #endasm
  72.  
  73. struct InputEvent *
  74. myhandler(ev, mydata)
  75.     register struct InputEvent    *ev;
  76.     register struct blanker        *mydata;
  77.     {
  78.  
  79.     mydata->Class = ev->ie_Class;
  80.     if (mydata->Class == IECLASS_TIMER)
  81.         {
  82.             mydata->Ticks++;
  83.             if (mydata->Ticks >= mydata->BlankPoint)
  84.                 {
  85.                 custom.dmacon = BITCLR | DMAF_RASTER | DMAF_COPPER;
  86.                  custom.color[0] = (UWORD) 0;    /* turn out the lights !  */
  87.                 mydata->Ticks = 0;                /* reset counter          */
  88.                 mydata->Dark = 1;                /* yes the lights are out */
  89.                 }
  90.         return(ev);
  91.         }
  92.     if (mydata->Class == IECLASS_RAWMOUSE)
  93.         {
  94.         mydata->Ticks = 0;
  95.         if (mydata->Dark)
  96.             {
  97.             custom.dmacon = BITSET | DMAF_RASTER | DMAF_COPPER; /* lightson */
  98.             mydata->Dark = 0;
  99.             }
  100.         return(ev);
  101.         }
  102.     if (mydata->Class == IECLASS_RAWKEY)
  103.         {
  104.         mydata->Ticks = 0;
  105.         if (mydata->Dark)
  106.             {
  107.             custom.dmacon = BITSET | DMAF_RASTER | DMAF_COPPER; /* lightson */
  108.             mydata->Dark = 0;
  109.             }
  110.         return(ev);
  111.         }
  112.     return(ev);
  113.     }
  114. /*    ----------------------------------------
  115.     End of event handler code
  116.     ---------------------------------------- */
  117.  
  118.  
  119.  
  120. main(argc, argv)
  121.     int        argc;
  122.     char    **argv;
  123.     {
  124.     struct MsgPort        *inputDevPort;        /* for opening input.device */
  125.     struct IOStdReq        *inputRequestBlock;    /* for opening input.device */
  126.     struct Interrupt    *handlerStuff;        /* for adding event handler */
  127.     long                n;                    /* for calc'ing handler size */
  128.     long                s;                    /* ticks till blanking */
  129.     struct blanker        *p;                    /* ptr to handler's variables */
  130.     char                *code;                /* prt to handler's entry point */
  131.  
  132.     if (argc > 1)
  133.         {
  134.         s = 10 * atoi(argv[1]);
  135.         if (s < 10)
  136.             {
  137.             s = 10;
  138.             }
  139.         }
  140.     else
  141.         {
  142.         s = DEFAULT_BLANKPOINT;
  143.         }
  144.  
  145.     inputDevPort = CreatePort("Blanker-Loader", 0);
  146.     if (inputDevPort == NULL) 
  147.         exit (1);
  148.  
  149.     inputRequestBlock = CreateStdIO(inputDevPort);
  150.     if (inputRequestBlock == 0)
  151.         {
  152.         DeletePort(inputDevPort);
  153.         exit (2);
  154.         }
  155.  
  156.     if (OpenDevice("input.device", 0, inputRequestBlock, 0))
  157.         {
  158.         DeleteStdIO(inputRequestBlock);
  159.         DeletePort(inputDevPort);
  160.         exit (3);
  161.         }
  162.  
  163.     handlerStuff = (struct Interrupt *)
  164.                             AllocMem (sizeof (struct Interrupt), MEMF_CLEAR);
  165.     if (handlerStuff == (struct Interrupt *) 0)
  166.         {
  167.         DeleteStdIO(inputRequestBlock);
  168.         DeletePort(inputDevPort);
  169.         exit (4);
  170.         }
  171.  
  172.     /* alloc space for the private variable space used by event handler */
  173.     p = (struct blanker *)AllocMem (sizeof (struct blanker), MEMF_CLEAR); 
  174.     if (p == 0)
  175.         {
  176.         DeleteStdIO(inputRequestBlock);
  177.         DeletePort(inputDevPort);
  178.         FreeMem (handlerStuff, sizeof (struct Interrupt));
  179.         exit (5);
  180.         }
  181.     p->Class = 0;
  182.     p->Ticks = 0;
  183.     p->BlankPoint = s;
  184.     p->Dark = 0;
  185.     strncpy (p->Name, "Blanker", 7);
  186.  
  187.     n = (char *)main - (char *)HandlerInterface; /* size of handler code */
  188.     code = AllocMem (n, MEMF_CLEAR); 
  189.     if (code == 0)
  190.         {
  191.         DeleteStdIO(inputRequestBlock);
  192.         DeletePort(inputDevPort);
  193.         FreeMem (handlerStuff, sizeof (struct Interrupt));
  194.         FreeMem (p, sizeof (struct blanker));
  195.         exit (6);
  196.         }
  197.     CopyMem (HandlerInterface, code, n); /* move handler code to safe spot */
  198.  
  199.     handlerStuff->is_Data = (APTR) p;         /* ptr to our variable space */
  200.     handlerStuff->is_Code = (VOID (*)())code; /* ptr to event handler code */
  201.     handlerStuff->is_Node.ln_Pri = 51;    /* priority just above Intuition */
  202.     handlerStuff->is_Node.ln_Name = p->Name;
  203.     inputRequestBlock->io_Command = IND_ADDHANDLER;
  204.     inputRequestBlock->io_Data = (APTR) handlerStuff;
  205.  
  206.     DoIO (inputRequestBlock);            /* install our event handler */    
  207.  
  208.     CloseDevice (inputRequestBlock);    /* close device and leave event */
  209.     DeleteStdIO (inputRequestBlock);    /* handler in memory             */
  210.     DeletePort (inputDevPort);
  211.  
  212.     if (argc > 0)
  213.         {
  214.         printf(" -- Blanker2 -- by Joe Hitchens -- V1.27.88 --\n");
  215.         printf("Screen Blanks after %d sec's of mouse/key inactivity.\n",
  216.                                                     s / 10);
  217.         }
  218.     }
  219.  
  220.  
  221.